<%@ page import="entity.User" %>
<%@ page import="java.util.*" %><%--
  Created by IntelliJ IDEA.
  User: Teacher
  Date: 2025/9/9
  Time: 14:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
<%
    // 模拟Java代码
    pageContext.setAttribute("msg", "Hello pageContext");
    request.setAttribute("msg", "Hello request");
    session.setAttribute("msg", "Hello session");
    application.setAttribute("msg", "Hello application");
    request.setAttribute("name", "admin");

    User user = new User(1, "admin", "123456", "13812345678", "江苏-南京");
    request.setAttribute("user", user);

    User user2 = new User(2, "jack", "111111", "13812345679", "江苏-苏州");
    User user3 = new User(3, "tom", "222222", "13812345688", "江苏-泰州");
    User user4 = new User(4, "lucy", "333333", "13812345888", "江苏-扬州");
    User user5 = new User(5, "john", "444444", "13812348888", "江苏-常州");
    List<User> list = new ArrayList<>(Arrays.asList(user, user2, user3, user4, user5));
    request.setAttribute("list", list);

    Map<String, User> map = new HashMap<>();
    map.put("u1", user);
    map.put("u2", user2);
    map.put("u3", user3);
    map.put("u4", user4);
    map.put("u5", user5);
    request.setAttribute("map", map);
%>
<h2>获取作用域数据</h2>
<!--
    可以通过作用域对象.key的方式获取指定作用域中指定key的数据
    可以直接通过key表示获取作用域的数据
    直接使用key的情况下,会自动搜索所有作用域中的数据
    其检索顺序为由小到大,只要在其中某一个作用域中找到了数据,则不再继续查找
    pageContext->request->session->application
    如果四大作用域中均没有对应数据,则什么都不显示
-->
pageContext:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg}<br>
application:${applicationScope.msg}<br>
name:${name}<br>
msg:${msg}<br>
password:${password}
<h2>获取应用名</h2>
应用名:<%=request.getContextPath()%><br>
应用名:<%
    HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
    String path = req.getContextPath();
    out.print(path);
%><br>
<!--
    在EL表达式中,如果遇到对象.getXxx的语法时,可以直接通过对象.xxx表示
-->
应用名:${pageContext.request.contextPath}
<h2>获取对象</h2>
user:${user}<br>
编号:${user.id}<br>
用户名:${user.username}<br>
密码:${user.password}<br>
电话:${user.phone}<br>
地址:${user.address}
<h2>访问List集合</h2>
${list[0]}<br>
${list[1]}<br>
${list[2]}<br>
${list[3]}<br>
${list[4].username}<br>
<h2>访问Map集合</h2>
${map['u1']}<br>
${map.u2}<br>
${map.u3.address}<br>
${map['u4'].phone}<br>
<h2>运算</h2>
${3+2}<br>
${3-2}<br>
${3*2}<br>
${3/2}<br>
${3%2}<br>
${3 == 2}<br>
${3 == "3"}<br>
${3 > 2 ? "3大于2" :"小于"}<br>
${3 > 2 and 2 > 3}<br>
${3 > 2 && 2 > 3}<br>
${3 > 2 or 2 > 3}<br>
${3 > 2 || 2 > 3}<br>
${empty user}<br>
${empty emp}<br>
${user == null}<br>
${emp == null}<br>

</body>
</html>
